import { useContext } from 'react';

import { defaultFormatResult, useRequest } from '@/hooks';
import { isSuccess } from '@/utils/request';

import { TabStoreContext } from '@/pages/SalaryManage/constant/TabStoreContext';
import { queryTabCount } from '@/services/salaryManage/salaryConfirm';
import { feedbackBusiType as TAB_KEY } from '@/pages/SalaryManage/enum';

const useFetchCountInfo = () => {
    const { mergeTabStore } = useContext(TabStoreContext);
    const { run: fetchCountInfo } = useRequest(param => queryTabCount(param), {
        manual: true,
        defaultFormatResult,
        onSuccess: res => {
            if (isSuccess(res)) {
                const { allCount, notAssignCount, notReceivedCount, notDealWithCount, doneCount } = res.data;

                const tabCountInfo = {
                    [TAB_KEY.ALL]: allCount,
                    [TAB_KEY.ASSIGN]: notAssignCount,
                    [TAB_KEY.RECEIVE]: notReceivedCount,
                    [TAB_KEY.HANDLE]: notDealWithCount,
                    [TAB_KEY.HANDLED]: doneCount
                };

                mergeTabStore({ tabCountInfo });
            }
        }
    });

    return fetchCountInfo;
};

export default useFetchCountInfo;
